#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
from ShopTrackingSpider import common
from scrapy import cmdline
from ShopTrackingSpider.spiders.dhgate.shop_spider import DhgateShop
from ShopTrackingSpider.utils.db_util import DBHelper
import MySQLdb


def test():
    # 初始化对象
    helper = DBHelper(DBHelper.local_host, DBHelper.user_name, DBHelper.pass_word, DBHelper.db_name)
    # 连接
    helper.connect()
    # sql
    sql = 'INSERT INTO  dhgate_merchandise  name = %s, platform = %s, categorie = %s'
    # params

    name = '123'
    platform = 'abc'
    categorie = '000'
    params = [name, platform, categorie]
    #执行
    # sql = """INSERT INTO dhgate_merchandise(
    #          name, platform, categorie)
    #          VALUES ('123', 'abc', '000')"""
    count = helper.insert(sql, params)
    # 判断
    if count:
        print('操作成功.')
    else:  # None,False,0
        print('操作失败.')
    # 打开数据库连接
    # db = MySQLdb.connect(DBHelper.local_host, DBHelper.user_name, DBHelper.pass_word, DBHelper.db_name, charset='utf8')
    #
    # # 使用cursor()方法获取操作游标
    # cursor = db.cursor()
    #
    # # SQL 插入语句
    # sql = """INSERT INTO dhgate_merchandise(
    #           name, platform, categorie)
    #           VALUES ('123', 'abc', '000')"""
    # try:
    #     # 执行sql语句
    #     cursor.execute(sql)
    #     # 提交到数据库执行
    #     db.commit()
    #     print('SQL成功')
    # except:
    #     # Rollback in case there is any error
    #     db.rollback()
    #     print('SQL失败')
    #
    #
    # # 关闭数据库连接
    # db.close()


def main():
    print('==========main start==========')
    # print(os.path.abspath('.'))
    # shop = DhgateShop()
    # shop.start()
    # cmdline.execute(['scrapy', 'crawl', 'DhgateShop'])
    # cmdline.execute(['scrapy', 'crawl', 'CategoriesList'])
    # cmdline.execute(['scrapy', 'crawl', 'CategoriesSpider'])
    # cmdline.execute(['scrapy', 'crawl', 'Demo'])

    test()

    print('==========main end==========')


if __name__ == '__main__':
    main()



